﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BL
{
    public class Topiki
    {
        static Topiki instance;
        public static Topiki Instance
        {
            get { return instance; }
        }

        List<Topic> topiki = new List<Topic>();
        long maxID = 0;

        public Topiki(List<Entity> topiki)
        {
            instance = this;
            foreach (Entity e in topiki)
            {
                this.topiki.Add((Topic)e);
                if (maxID < e.ID)
                    maxID = e.ID;
            }
            maxID++;
        }

        public Topic[] Items
        {
            get { return topiki.ToArray(); }
        }

        public void Add(Topic t)
        {
            topiki.Add(t);
            if (maxID < t.ID)
                maxID = t.ID;
            maxID++;
        }

        public long FreeID
        {
            get { return maxID; }
        }

        public Topic this[long id]
        {
            get
            {
                foreach (Topic t in topiki)
                {
                    if (t.ID == id)
                        return t;
                }
                return null;
            }
            set
            {
                for (int i = 0; i < topiki.Count; i++)
                {
                    if (topiki[i].ID == id)
                    {
                        topiki[i] = value;
                        return;
                    }
                }
            }
        }

        public List<Topic> SelectSubject(long subjectID)
        {
            List<Topic> list = new List<Topic>();
            foreach (Topic top in topiki)
            {
                if (top.Subject.ID == subjectID)
                    list.Add(top);
            }
            return list;
        }
        

        public void Delete(long id)
        {
            Topic t = this[id];
            topiki.Remove(t);
        }
    }
}
